package com.loongtech.bi.manager.system;

import com.loongtech.bi.entity.system.EntityBiChannel;
import com.loongtech.bi.entity.system.EntitySysRoleChannel;
import com.loongtech.bi.entity.system.EntitySysRoleFunction;
import com.loongtech.core.jpa.manager.ManagerBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service("sysRoleChannelManager")
public class SysRoleChannelManager extends ManagerBase<EntitySysRoleChannel> {
    private static final long serialVersionUID = 9L;

    @Autowired
    private BiChannelManager channelManager;
    @Autowired
    private SysUserManager sysUserManager;
    @Resource
    private SysRoleFunctionManager sysRoleFunctionManager;

    /**
     * 根据userid获取授权的渠道标识集合
     *
     * @return
     */
    public HashSet<String> getChannelSetByUserId(int userId) {
        HashSet<String> chanSet = new HashSet<>();

        //对超级管理员特殊处理
        if (sysUserManager.isAdmin(userId) || userId == 0) {
            for (EntityBiChannel channel : channelManager.findAll()) {
                chanSet.add(channel.getId());
            }
            return chanSet;
        }

        List<Object> list = findByQuery("select rc.channelId from EntitySysRoleChannel as rc, EntitySysUserRole as ur, EntitySysRole as sr where rc.roleId = sr.id and ur.roleId = sr.id and ur.userId=?", userId);
        if (null == list || list.isEmpty()) {
            return chanSet;
        }

        for (Object channelId : list) {
            if (null != channelId) {
                chanSet.add(channelId.toString());
            }
        }
        return chanSet;
    }

    /**
     * 获取一个角色当前已经被授权的渠道信息
     *
     * @param roleId
     * @return
     */
    public Set<String> getRolechannelSet(int roleId) {
        Set<String> set = new HashSet<>();
        List<EntitySysRoleChannel> list = findBy(EntitySysRoleChannel.K.roleId, roleId);
        if (null != list && !list.isEmpty()) {
            for (EntitySysRoleChannel channel : list) {
                set.add(channel.getChannelId());
            }
        }
        return set;
    }

    /**
     * 获得角色当前归属的渠道列表
     *
     * @param roleId
     * @return
     */
    public List<EntityBiChannel> getRoleChannelList(int roleId) {
        List<EntityBiChannel> list = new ArrayList<EntityBiChannel>();
        for (EntitySysRoleChannel info : findBy(EntitySysRoleChannel.K.roleId, roleId)) {
            EntityBiChannel channel = channelManager.get(info.getChannelId());
            if (null != channel) {
                list.add(channel);
            }
        }
        return list;
    }

    /**
     * 删除一条记录
     *
     * @param roleId
     * @param channelId
     * @return
     */
    public boolean removeByRoleIdAndChannelId(int roleId, String channelId) {
        EntitySysRoleChannel entity = getBy(EntitySysRoleChannel.K.roleId, roleId, EntitySysRoleChannel.K.channelId, channelId);
        if (null != entity) {
            removeById(entity.getId());
            return true;
        }

        return false;
    }

    /**
     * 插入一条记录
     *
     * @param roleId
     * @return
     */
    public boolean insertByRoleIdAndChannelId(int roleId, String channelId) {
        EntitySysRoleChannel entity = getBy(EntitySysRoleChannel.K.roleId, roleId, EntitySysRoleChannel.K.channelId, channelId);
        if (null == entity) {
            entity = new EntitySysRoleChannel();
            entity.setRoleId(roleId);
            entity.setChannelId(channelId);
            persist(entity);
            return true;
        }

        return false;
    }

    /**
     * 判断一个渠道是否已经分配
     *
     * @param channelId
     * @return
     */
    public boolean hasChannelAssigned(String channelId) {
        if (null == channelId || channelId.trim().isEmpty()) {
            return false;
        }
        List<EntitySysRoleChannel> list = findBy(EntitySysRoleChannel.K.channelId, channelId);
        return null != list && list.size() > 0;
    }

    /**
     * 判断一个渠道是否已经分配
     *
     * @param channelId
     * @return
     */
    public boolean hasChannelAssignedNew(String channelId) {
        if (null == channelId || channelId.trim().isEmpty()) {
            return false;
        }
        String channelName = getByQuery("select name from EntityBiChannel where id = ? ", channelId);
        List<EntitySysRoleFunction> list = sysRoleFunctionManager.findByQuery("from EntitySysRoleFunction where filter1 = ? or filter2 = ? or filter3 = ?", channelName, channelName, channelName);
        return null != list && list.size() > 0;
    }
}
